import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Collection;
import java.util.Properties;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.stream.XMLStreamWriter;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;



import org.w3c.dom.Document;
import org.w3c.dom.Element;


public class Main {
	final static String ROOT = "sentenses";
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		FileReader fr;
		File file = new File(args[0]);
		try {
			fr = new FileReader(file);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			System.out.println("File " + args[0] + " not found");
			return;
			//e.printStackTrace();
		}
		BufferedReader reader = new BufferedReader(fr);
		final Dictionary dict = new Dictionary();
		String line;
		try {
			while ((line = reader.readLine()) != null) {
				String[] elements = line.split(";");
				if (elements.length == 3 && Integer.valueOf(elements[2]) > 3) {
					
					dict.add(elements[0], elements[1], Integer.valueOf(elements[2]));
				}
				if (elements.length == 4 && Integer.valueOf(elements[3]) > 3 ) {
					dict.add(elements[0], elements[1], Integer.valueOf(elements[3]));
					dict.add(elements[0], elements[2], Integer.valueOf(elements[3]));
				}
			}
			
		reader = new BufferedReader(new FileReader("./data/result_text"));
		String text = "";
		while ((line = reader.readLine()) != null) {
			if (!line.startsWith(".")) {
				text += line + " ";
			} else {
				text += ".";
			}
		}
		
		try {
			Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
			Element root = doc.createElement(ROOT);
			long time = 0;
			long delta = 0;
			String[] sentenses = text.split("\\.");
			float average = 0;
			int count = 0;
			for (String sentence : sentenses ) {
				 time = System.currentTimeMillis();
				final SentenceParser s = new SentenceParser(sentence, dict);
				if (s.getSizeSentense() > 1) {
					average += s.getCoverage();
					count += 1;
				}
				root.appendChild(s.toNodeXml(doc));
				delta += System.currentTimeMillis() - time;
				
				//System.out.print(s.toString() + "\n");
				
			}
			System.out.println(average / count);
			doc.appendChild(root);
			
		
			
			Source domSource = new DOMSource(doc);
			Result fileResult = new StreamResult(new File("./data/output.xml").toURI().getPath());
			TransformerFactory factory = TransformerFactory.newInstance();
	        factory.setAttribute("indent-number", new Integer(4));

		//	factory.setAttribute("indent-number", 	new Integer(4));
			Transformer transformer = factory.newTransformer();
	        

			transformer.setOutputProperty(OutputKeys.INDENT, "yes");
			transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");

			
			transformer = factory.newTransformer();
			

			transformer.transform(domSource, fileResult);
			 
		} catch (TransformerConfigurationException e) {
				// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch (TransformerException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
			
		
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
				
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	
	}
	

}
